import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions,URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
// declare var $: any;
import * as $ from 'jquery';
// import { Post } from './model/post-model';
@Injectable()
export class ConmonService{
    token:string = "";
    employId:string = "";
    plpName:string = '';
    SumMyApplyWorkInfo:string = "";
    SumBacklogListInfo:string = "";
    SumProjectWorkListInfo:string = "";
    IsProjectManager:string = "";
    // url:string = "http://crmdev.afcat.com.cn:8001/psc/CRMDEV/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
    // url:string = "http://crmsit.afcat.com.cn:8001/psc/CRMSIT/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
    url:string = "http://crm.afcat.com.cn/psc/AFCATCRM/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
    private domainVal:string = "afcat.com.cn";
    personScreeningData:any;
    projectScreeningData:any;
    personScreeningPostData:any;
    projectScreeningPostData:any;
    openId:any;
    constructor(private router: Router) {};
    getToken(num,openId,serviceName,flag) {
        console.log('这是service里的方法');
        this.openId = openId;
        let _this = this;
        document.domain = 'afcat.com.cn';        
        // var token,employId;
        var strRemoteAddr = '10.25.40.67';
        var obj = {};
        $.ajax({
            async: false, // 同步请求 false，异步请求 true
            type: "POST", // 请求类型，POST、GET
            // url: "http://10.25.36.200:8080/cms/api/getEncryptInfoAndStaffNumForPms", // 请求的地址
            url: "http://221.122.75.141/cms/api/getEncryptInfoAndStaffNumForPms",
            data: {
                // openId: "NTk5OS8mL0MxO2Q8NDtoOzs4ZTQ2ZmRlaDM0OGU3NDZpZjk4ZTU0Ojg1",
                openId: openId,
                remoteIp: strRemoteAddr
            },
            dataType: 'json',
            success: function(responseData, textStatus, XMLHttpRequest) {
            _this.token = responseData.body.encryptInfo;
            _this.employId = responseData.body.staffNum;
                console.log(responseData);
                // if (flag) {
                //     flag = false;
                //     setCookie(token, employId);
                // };
                // getEmployId(token, openId)
                if (flag) {
                    _this.withoutBodyData(serviceName);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // 处理请求发生异常
                console.log("请求发生异常！" + textStatus + errorThrown);
            },
        });
        // obj = {
        //     token:_this.token,
        //     employId:_this.employId
        // }
        // return obj;
    };
    withoutBodyData (serviceName) {
        let d = new Date();
        // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
        d.setTime(d.getTime() + (60 * 60 * 1000));
        console.log(d);
        let expires = "expires=" + d.toUTCString();
        console.log(this.token);
        document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
        console.log(document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";")
        let _this = this;
        let dataList;
        $.ajax({
            async: false, // 同步请求 false，异步请求 true
            type: "POST", // 请求类型，POST、GET
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url: _this.url, // 请求的地址
            // data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':'00001241'},'body':{}}"},
            data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.employId+"'"+"},'body':{}}"},
            dataType: 'json',
            success: function(responseData) {
                console.log(responseData);
                dataList = responseData;
                _this.employId = responseData.body.employId;
                _this.plpName = responseData.body.PplName;
                _this.SumMyApplyWorkInfo = responseData.body.SumMyApplyWorkInfo;
                _this.SumBacklogListInfo = responseData.body.SumBacklogListInfo;
                _this.SumProjectWorkListInfo = responseData.body.SumProjectWorkListInfo;
                _this.IsProjectManager = responseData.body.IsProjectManager;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // 处理请求发生异常
                console.log("请求发生异常！" + textStatus + errorThrown);
            },
        });
        return dataList;
    };
    withBodyData (dataObj,serviceName) {
        // let d = new Date();
        // // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
        // d.setTime(d.getTime() + (5 * 1000));
        // let expires = "expires=" + d.toUTCString();
        // document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
        let _this = this;
        dataObj = JSON.stringify(dataObj);
        console.log(dataObj);
        let dataList;
        $.ajax({
            async: false, // 同步请求 false，异步请求 true
            type: "POST", // 请求类型，POST、GET
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url: _this.url, // 请求的地址
            // data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':'000123'},'body':"+dataObj+"}"},
            data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.employId+"'"+"},'body':"+dataObj+"}"},
            dataType: 'json',
            success: function(responseData) {
                console.log(responseData);
                dataList = responseData;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // 处理请求发生异常
                console.log("请求发生异常！" + textStatus + errorThrown);
            },
        });
        return dataList;
    };
    savePersonScreeningData (dataObj,serviceName,flag) {
        // let d = new Date();
        // // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
        // d.setTime(d.getTime() + (5 * 1000));
        // let expires = "expires=" + d.toUTCString();
        // document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
        let _this = this;
        dataObj = JSON.stringify(dataObj);
        let dataList;
        $.ajax({
            async: false, // 同步请求 false，异步请求 true
            type: "POST", // 请求类型，POST、GET
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url: _this.url, // 请求的地址
            // data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':'000123'},'body':"+dataObj+"}"},
            data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.employId+"'"+"},'body':"+dataObj+"}"},
            dataType: 'json',
            success: function(responseData) {
                console.log(responseData);
                if (responseData.head.Result === 'S') {
                    _this.personScreeningData = responseData;
                    _this.router.navigate(['./PersonTatal', flag]);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // 处理请求发生异常
                console.log("请求发生异常！" + textStatus + errorThrown);
            },
        });
    };
    saveProjectScreeningData (dataObj,serviceName) {
        // let d = new Date();
        // // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
        // d.setTime(d.getTime() + (5 * 1000));
        // let expires = "expires=" + d.toUTCString();
        // document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
        let _this = this;
        dataObj = JSON.stringify(dataObj);
        let dataList;
        $.ajax({
            async: false, // 同步请求 false，异步请求 true
            type: "POST", // 请求类型，POST、GET
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url: _this.url, // 请求的地址
            data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.employId+"'"+"},'body':"+dataObj+"}"},
            dataType: 'json',
            success: function(responseData) {
                console.log(responseData);
                _this.projectScreeningData = responseData;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // 处理请求发生异常
                console.log("请求发生异常！" + textStatus + errorThrown);
            },
        });
    };
}